package com.xlro.dao;

import com.xlro.db.JDBCUtil;
import com.xlro.domain.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserDao {
    public ArrayList<User> getAll() {
        ArrayList<User> users = null;
        try (
                ResultSet resultSet = JDBCUtil.getInstance().executeQueryRS("SELECT * FROM `borrow_card`", null)){
            User user = null;
            users = new ArrayList<>();
            while (resultSet.next()){
                user = new User();
                user.setId(resultSet.getString(1));
                user.setPassword(resultSet.getString(2));
                user.setReader(resultSet.getString(3));
                user.setRule_id(resultSet.getString(4));
                user.setStatus(resultSet.getString(5));
                user.setUsername(resultSet.getString(6));
                user.setHeader(resultSet.getString(7));
                user.setDescribe(resultSet.getString(8));
                user.setCellphone(resultSet.getString(9));
                user.setEmail(resultSet.getString(10));
                user.setSex(resultSet.getString(11));
                users.add(user);
            }
        } catch(SQLException e){
            e.printStackTrace();
        }

        return users;
    }

    public int alterPass(String id,String pass) {
        return JDBCUtil.getInstance().executeUpdate("UPDATE borrow_card set borrow_card.`password` = ? where borrow_card.id = ?"
                , new Object[]{pass,id});
    }

    public int alterInfo(String id, String username, String reader, String desc, String cell, String email, String sex) {

//        System.out.println("女".equals(sex)?"1":"0");

        return JDBCUtil.getInstance().executeUpdate("UPDATE borrow_card set borrow_card.username = ?" +
                        ",borrow_card.reader = ? ,borrow_card.`describe`= ? " +
                        ", borrow_card.cellphone = ? , borrow_card.email = ? " +
                        ", borrow_card.sex = ? where borrow_card.id= ? "
                , new Object[]{username,reader,desc,cell,email,sex,id});
    }

    public int changeStatus(String id) {
        return JDBCUtil.getInstance().executeUpdate("UPDATE borrow_card set borrow_card.`status` = '0' WHERE id=?"
                , new Object[]{id});
    }

    public int deleteOne(String id) {
        return JDBCUtil.getInstance().executeUpdate("DELETE FROM `borrow_card` WHERE id = ?"
                , new Object[]{id});
    }
}
